﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Xml.Linq;
using iShoppingDAO;
using iShoppingDTO;
namespace iShoppingBUS
{
    public class ChiTietGiaoDichBUS
    {
        public static List<ChiTietGiaoDichDTO> LayThongTinDauGiaTheoMaTaiKhoan(string mataikhoan)
        {
            List<ChiTietGiaoDichDTO> dsCTGD = new List<ChiTietGiaoDichDTO>();
            DataTable dt = ChiTietGiaoDichDAO.LayThongTinDauGiaTheoMaTaiKhoan(mataikhoan);
            int n = dt.Rows.Count;
            foreach (DataRow dr in dt.Rows)
            {
                ChiTietGiaoDichDTO ctgd = new ChiTietGiaoDichDTO();
                ctgd.MaChiTietGiaoDich = dr["MaChiTietGiaoDich"].ToString();
                ctgd.GiaGiaoDich = int.Parse(dr["GiaGiaoDich"].ToString());
                ctgd.ThoiGianGiaoDich = DateTime.Parse(dr["ThoiGianGiaoDich"].ToString());
                ctgd.TinhTrang = dr["TinhTrang"].ToString();
                ctgd.ThongTinSanPham = new SanPhamDTO();
                ctgd.ThongTinSanPham.MaSanPham = dr["MaSanPham"].ToString();
                ctgd.ThongTinSanPham.TenSanPham = dr["TenSanPham"].ToString();
                ctgd.ThongTinSanPham.GiaKhoiDiem = int.Parse(dr["GiaKhoiDiem"].ToString());
                ctgd.ThongTinSanPham.GiaHienTai = int.Parse(dr["GiaHienTai"].ToString());
                ctgd.ThongTinSanPham.NgayHetHan = DateTime.Parse(dr["NgayHetHan"].ToString());
                ctgd.ThongTinSanPham.HinhAnh = dr["HinhAnh"].ToString();
                ChiTietGiaoDichDTO temp = null;
                bool check = false;
                foreach (ChiTietGiaoDichDTO ct in dsCTGD)
                {
                    if (ctgd.ThongTinSanPham.MaSanPham == ct.MaSanPham)
                    {
                        check = true;
                        if (ctgd.GiaGiaoDich > ct.GiaGiaoDich)
                        {
                            temp = ct;
                        }
                        break;
                    }
                }
                if (temp != null)
                {
                    dsCTGD.Remove(temp);
                    check = false;
                }
                if (!check)
                    dsCTGD.Add(ctgd);
            }
            return dsCTGD;
        }

        public static List<ChiTietGiaoDichDTO> LayThongTinDauGiaTheoSanPham(string masanpham)
        {
            List<ChiTietGiaoDichDTO> dsCTGD = new List<ChiTietGiaoDichDTO>();
            DataTable dt = ChiTietGiaoDichDAO.LayThongTinDauGiaTheoSanPham(masanpham);
            int n = dt.Rows.Count;
            foreach (DataRow dr in dt.Rows)
            {
                ChiTietGiaoDichDTO ctgd = new ChiTietGiaoDichDTO();
                ctgd.TenTaiKhoan = dr["TenTaiKhoan"].ToString();
                ctgd.MaChiTietGiaoDich = dr["MaChiTietGiaoDich"].ToString();
                ctgd.GiaGiaoDich = int.Parse(dr["GiaGiaoDich"].ToString());
                ctgd.ThoiGianGiaoDich = DateTime.Parse(dr["ThoiGianGiaoDich"].ToString());
                ctgd.TinhTrang = dr["TinhTrang"].ToString();
                dsCTGD.Add(ctgd);
            }
            return dsCTGD;
        }

        public static List<string> LaySanPhamTheoMaTaiKhoan(string mataikhoan)
        {
            List<string> listmasanpham = new List<string>();
            DataTable dt = ChiTietGiaoDichDAO.LaySanPhamTheoMaTaiKhoan(mataikhoan);
            foreach (DataRow dr in dt.Rows)
            {
                listmasanpham.Add(dr["MaSanPham"].ToString());
            }

            return listmasanpham;
        }

        public static List<ChiTietGiaoDichDTO> LayThongTinDauGiaTheoSanPhamTaiKhoan(string masanpham, string mataikhoan)
        {
            List<ChiTietGiaoDichDTO> dsCTGD = new List<ChiTietGiaoDichDTO>();
            DataTable dt = ChiTietGiaoDichDAO.LayThongTinDauGiaTheoSanPhamTaiKhoan(masanpham, mataikhoan);
            foreach (DataRow dr in dt.Rows)
            {
                ChiTietGiaoDichDTO ctgd = new ChiTietGiaoDichDTO();
                ctgd.TenTaiKhoan = dr["TenTaiKhoan"].ToString();
                ctgd.MaChiTietGiaoDich = dr["MaChiTietGiaoDich"].ToString();
                ctgd.GiaGiaoDich = int.Parse(dr["GiaGiaoDich"].ToString());
                ctgd.ThoiGianGiaoDich = DateTime.Parse(dr["ThoiGianGiaoDich"].ToString());
                ctgd.TinhTrang = dr["TinhTrang"].ToString();
                dsCTGD.Add(ctgd);
            }
            return dsCTGD;
        }
         public static List<ChiTietGiaoDichDTO> SelectChiTietSP(string masanpham, string mataikhoan)
         {
             return ChiTietGiaoDichDAO.SelectChiTietSP(masanpham, mataikhoan);
         }
        public static bool UpdateTrangThai(ChiTietGiaoDichDTO chiTietGiaoDichDto)
        {
            return ChiTietGiaoDichDAO.UpdateTrangThai(chiTietGiaoDichDto);
        }
    }
}
